package com.bb.blog.activiti.controller.query;

import com.bb.blog.activiti.domain.ValidGroup;
import com.bb.blog.activiti.domain.dto.ProcessDefObj;
import com.bb.blog.activiti.domain.dto.ProcessInsObj;
import com.bb.blog.activiti.domain.param.StartProcessParam;
import lombok.RequiredArgsConstructor;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.springframework.beans.BeanUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 流程
 */
@RequestMapping("/process")
@RestController
@RequiredArgsConstructor
public class ProcessController {
    private final RepositoryService repositoryService;
    private final RuntimeService runtimeService;


    /**
     * 查询流程定义
     * @return
     */
    @GetMapping("/definition/list")
    public Object processDefinitionList() {
        List<ProcessDefinition> processDefinitionList = repositoryService.createProcessDefinitionQuery().list();
        return processDefinitionList.stream().map(processDefinition -> {
            ProcessDefObj po = new ProcessDefObj();
            BeanUtils.copyProperties(processDefinition, po);
            return po;
        }).collect(Collectors.toList());
    }

    /**
     * 查询流程实例
     * @param processDefinitionId
     * @return
     */
    @GetMapping("/instance/list")
    public Object processInstanceList(
            @RequestParam String processDefinitionId
    ) {
        List<ProcessInstance> list = runtimeService.createProcessInstanceQuery().processDefinitionId(processDefinitionId).list();
        List<ProcessInsObj> processInsObjList = list.stream().map(processInstance -> {
            ProcessInsObj po = new ProcessInsObj();
            BeanUtils.copyProperties(processInstance, po);
            return po;
        }).collect(Collectors.toList());
        return processInsObjList;
    }


}
